package com.example.communist_app_api.db.dao;

import com.example.communist_app_api.db.pojoDto.NoticeRefEntity;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <h4>community-uniapp</h4>
 * <p></p>
 *
 * @author : Lowell
 * @date : 2023-01-15 04:11
 **/

@Repository
public class NoticeRefDao {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private UserDao userDao;

    public static final String RECEIVER_ID="receiverId";

    public static final String READ_FLAG="readFlag";

    public static final String LAST_FLAG="lastFlag";
    public static final String COLLECTION_NAME="notice_ref";
    public String insert(NoticeRefEntity entity){

        entity= mongoTemplate.save(entity);
        return entity.get_id();
    }
    //查询未读消息的数量(汇总统计都是long类型)
    public long searchUnreadCount(int userId){
        Query query=new Query();
        query.addCriteria(Criteria.where(READ_FLAG).is(false).and(RECEIVER_ID).is(userId));
        long count = mongoTemplate.count(query, NoticeRefEntity.class);
        return count;
    }
    //修改更新条数
    public long searchLastCount(int userId){
        Query query=new Query();
        query.addCriteria(Criteria.where(LAST_FLAG).is(true).and(RECEIVER_ID).is(userId));
        Update update=new Update();

        update.set(LAST_FLAG,false);
        UpdateResult result = mongoTemplate.updateMulti(query, update, COLLECTION_NAME);
        long count = result.getModifiedCount();
        return count;
    }
    //修改消息状态，未读改为已读
    public long updateUnreadCount(String id){
        Query query=new Query();
        query.addCriteria(Criteria.where("_id").is(id));
        Update update=new Update();

        update.set(READ_FLAG,true);
        UpdateResult result = mongoTemplate.updateFirst(query, update, COLLECTION_NAME);
        long count = result.getModifiedCount();
        return count;
    }
    //根据主键值删除
    public long deleteNoticeRefById(String id){
        Query query=new Query();
        query.addCriteria(Criteria.where("_id").is(id));
        DeleteResult result = mongoTemplate.remove(query, COLLECTION_NAME);
        long count = result.getDeletedCount();
        return count;

    }
    //根据UserId删除所有消息
    public long deleteUserNoticeRef(int userId){
        Query query=new Query();
        query.addCriteria(Criteria.where(RECEIVER_ID).is(userId));
        DeleteResult result = mongoTemplate.remove(query, COLLECTION_NAME);
        long count = result.getDeletedCount();
        return count;

    }


    public List<String> selectAlreadyReadUser(String id) {
        Query query=new Query();
        query.addCriteria(Criteria.where(READ_FLAG).is(true).and("noticeId").is(id));
        List<NoticeRefEntity> noticeRefEntities = mongoTemplate.find(query, NoticeRefEntity.class);
        List<Integer> userIdList = noticeRefEntities.stream().map(NoticeRefEntity::getReceiverId).collect(Collectors.toList());
        List<String> userNameList = userDao.getUserNameListByUserIdList(userIdList);
        return userNameList;
    }
}
